package com.lznb.cloud.controller;

import com.lznb.cloud.entity.FileEntity;
import com.lznb.cloud.utils.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/uploadController")
@RestController
public class UploadController {

    @Value("${web.upload-path}")
    private String path;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    Map upload(@RequestParam(value = "file") MultipartFile file) {
        Map map=new HashMap();
        if (!file.isEmpty()) {
            try {
                String filename = file.getOriginalFilename();
//                int indexOf = filename.lastIndexOf(".");
//                String format=filename.substring(indexOf);
                String datetime = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
                filename=datetime+"_"+filename;
                File fileToSave = new File(path + filename);
                file.transferTo(fileToSave);
                map.put("code","1");
                map.put("filename","/"+filename);
                return map;
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code","0");
            }
        }
        return map;
    }




    /**
     * 上传图片
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/ueUpload")
    public Map<String, Object> images (MultipartFile upfile, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> params = new HashMap<String, Object>();
        try{
            Map map=new HashMap();
            if (!upfile.isEmpty()) {
                try {
                    String filename = upfile.getOriginalFilename();
                    int indexOf = filename.lastIndexOf(".");
                    String format=filename.substring(indexOf);
                    String datetime = org.apache.commons.lang.time.DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
                    filename=datetime+format;
                    File fileToSave = new File(path + filename);
                    upfile.transferTo(fileToSave);
                    params.put("state", "SUCCESS");
                    params.put("url", "/"+filename);
                    params.put("size", upfile.getSize());
                    params.put("original", "/"+filename);
                    params.put("type", upfile.getContentType());
                } catch (IOException e) {
                    e.printStackTrace();
                    params.put("state", "ERROR");
                }
            }


        } catch (Exception e){
            params.put("state", "ERROR");
        }
        return params;
    }

}
